package com.invengo.module_robot.ui.fragment.popular;

import static android.widget.AbsListView.OnScrollListener.SCROLL_STATE_IDLE;
import static androidx.viewpager2.widget.ViewPager2.SCROLL_STATE_DRAGGING;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.invengo.library_orionstar.RobotConstant;
import com.invengo.library_orionstar.RobotManager;
import com.invengo.module_robot.BR;
import com.invengo.module_robot.R;
import com.invengo.module_robot.databinding.FragmentPopularBinding;
import com.invengo.module_robot.ui.fragment.weiget.LinearTopSmoothScroller;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * @author buoker
 * @version 1.0.0
 * @date 2024/03/20
 * @className java.lang.String
 */
public class PopularFragment extends BaseFragment<FragmentPopularBinding, PopularViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_popular;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {

        new Handler().postDelayed(() -> {
            RobotManager.getInstance().changeTip(RobotConstant.TIP_POPULAR);
            viewModel.getPopular();
        },300);
    }

    /**
     * 初始视图可观察
     */
    @Override
    public void initViewObservable() {
        viewModel.ui.itemEvent.observe(this, bean -> {
            Bundle bundle = new Bundle();
            bundle.putString("publisher",bean.getPublisher()+"");
            bundle.putString("author",bean.getAuthor()+"");
            bundle.putString("bookName",bean.getBookName()+"");
            bundle.putString("isbn",bean.getIsbn()+"");
            bundle.putString("image",bean.getImage()+"");
            bundle.putString("content",bean.getRemark()+"");
            NavHostFragment.findNavController(this).navigate(R.id.action_popularFragment_to_bookInfoFragment,bundle);
        });
        viewModel.ui.moveEvent.observe(this, integer -> scrollItemToTop(integer));
        binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == SCROLL_STATE_IDLE || newState == SCROLL_STATE_DRAGGING) {
                    // DES: 找出当前可视Item位置
                    RecyclerView.LayoutManager layoutManager = binding.recyclerView.getLayoutManager();
                    if (layoutManager instanceof GridLayoutManager) {
                        LinearLayoutManager manager = (GridLayoutManager) layoutManager;
                        int mFirstVisiblePosition = manager.findFirstVisibleItemPosition();
                        viewModel.setCurrentPosition(mFirstVisiblePosition);
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                viewModel.setMove2Buttom(!recyclerView.canScrollVertically(2));
            }
        });
    }

    @Override
    public void onDestroyView() {
        binding.speechView.unRegister();
        super.onDestroyView();
    }

    /**
     * 指定item并置顶
     *
     * @param position item索引
     */
    public void scrollItemToTop(int position) {
        LinearTopSmoothScroller smoothScroller = new   LinearTopSmoothScroller(getContext(),false);
        smoothScroller.setTargetPosition(position);
        binding.recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
    }

}
